package cn.com.dashihui.web.controller;

import java.io.IOException;

import com.jfinal.aop.Duang;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

import cn.com.dashihui.kit.CommonKit;
import cn.com.dashihui.kit.DatetimeKit;
import cn.com.dashihui.kit.DirKit;
import cn.com.dashihui.kit.ValidateKit;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.AdminSeller;
import cn.com.dashihui.web.dao.Seller;
import cn.com.dashihui.web.service.SellerService;
public class SellerController extends BaseController{
	private SellerService sellerService = Duang.duang(SellerService.class);
	public void index(){
		//获取当前登录用户的id
	    int userid=getCurrentUser().getInt("id");
	    //查询当前用户的是否已经注册过sellerid
	    Record record=sellerService.FindById(userid);	    
	    if(record!=null){
	    	String sellerId=record.getStr("sellerId"); 	
	    	Record object=sellerService.detail(sellerId, userid);
	    	setAttr("userid", userid);
	    	setAttr("object", object);
	        render("detail.jsp");
	    }else{			
			String selleridd="SE"+userid+CommonKit.buildRandomString();
			setAttr("userid", userid);
			setAttr("sellerid", selleridd);
	        render("index.jsp");
		}
		
	   }
	  /**
	   * 添加公司
	   */
	  public void doAdd(){
	  
		  //营业执照照片
		   UploadFile busthumb = getFile("busthumb");
		   //法人照片
			UploadFile perthumb = getFile("perthumb");
			//商户ID
		   String sellerid=getPara("sellerid");
		   //用户id
		   AdminSeller user=getCurrentUser();
		   if (user==null) {
			   renderFailed("请重新登录");
			   return;
		   }
		   int userid = user.get("id");
		   //商户名称
			String sellerName = getPara("sellerName");
			//公司简称
			String shortName = getPara("shortName");
			//企业法人
			String legalPerson = getPara("legalPerson");
			//联系方式
			String contact = getPara("contact");
			//营业执照编号
			String busLicense = getPara("busLicense");
			//创建时间
			String createtime=DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss");
			//成立时间
			String settime = getPara("settime");
			//开店权限：1：开启；2：关闭
			int power=1;
			int state=1;
			if(sellerService.FindExistsById(sellerid)){
				renderResult(1);
				return;				
			}else if(!ValidateKit.Tel(contact) && !ValidateKit.Mobile(contact)){
				renderResult(2);
				return;		
			}
			else if(!ValidateKit.Datetime(settime)){
				renderResult(3);
				return;		
			}
			else if(!StrKit.notNull(busthumb)){
				renderResult(4);
				return;	
			}
			else if(!StrKit.notNull(busthumb)){
				renderResult(5);
				return;	
			}
			else if(sellerService.findUser(contact)){
				renderResult(6);
				return;	
			}
		 else{
			  Seller seller =new Seller()
					.set("sellerId", sellerid)
					.set("userid", userid)
					.set("shortName", shortName)
					.set("sellerName", sellerName)
					.set("legalPerson", legalPerson)
					.set("contact", contact)
					.set("createtime", createtime)
					.set("busLicense", busLicense)
					.set("settime", settime)
					.set("state", state)
					.set("power", power);			
				//如果上传了图片，则上传至FTP，并记录图片文件名
				if(perthumb!=null&&busthumb!=null){
					String thumbFileName;
					String bustFileName;				
					String dir = DirKit.getDir(DirKit.STORE_LOGO);
					String dirr = DirKit.getDir(DirKit.STORE_LOGO);
					try {
						thumbFileName = uploadToFtp(dir,busthumb);
						bustFileName = uploadToFtp(dirr,perthumb);
					} catch (IOException e) {
						e.printStackTrace();
						renderResult(4);
						return;
					}
					seller.set("busthumb", dir.concat(thumbFileName));
					seller.set("perthumb", dirr.concat(bustFileName));
				}
				
				if(seller.save()){
					renderSuccess();
					//redirect("/dict/seller/index");
					   //render("dict/seller/index.jsp");
					return;
				}
		 }
				renderFailed(); 
	  } 
	  
	  
	  /**
	   * 被驳回重新编辑提交公司
	   */
	  public void doEdit(){
	  
		  //营业执照照片
		   UploadFile busthumb = getFile("busthumb");
		   //法人照片
			UploadFile perthumb = getFile("perthumb");
			//商户ID
			String sellerid=getPara("sellerid");
			//用户id
			AdminSeller user=getCurrentUser();
			int userid=user.getInt("id");
			//商户名称
			String sellerName = getPara("sellerName");
			//公司简称
			String shortName = getPara("shortName");
			//企业法人
			String legalPerson = getPara("legalPerson");
			//联系方式
			String contact = getPara("contact");
			//营业执照编号
			String busLicense = getPara("busLicense");
			String busthumbOld = getPara("busthumbOld");
			String perthumbOld = getPara("perthumbOld");
			//创建时间
			String createtime=DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss");
			//成立时间
			String settime = getPara("settime");
			//开店权限：1：开启；2：关闭
			int power=1;
			int state=6;
			if(!ValidateKit.Tel(contact) && !ValidateKit.Mobile(contact)){
				renderResult(12);
			}else{
				Seller seller =sellerService.findBySellerId(sellerid)
					//.set("sellerId", sellerid)
					.set("userid",userid)
					.set("shortName", shortName)
					.set("sellerName", sellerName)
					.set("legalPerson", legalPerson)
					.set("contact", contact)
					.set("createtime", createtime)
					.set("busLicense", busLicense)
					.set("settime", settime)
					.set("state", state)
					.set("power", power);			
				//如果上传了图片，则上传至FTP，并记录图片文件名
				if(perthumb!=null&&busthumb!=null){
					String thumbFileName;
					String bustFileName;				
					String dir = DirKit.getDir(DirKit.STORE_LOGO);
					String dirr = DirKit.getDir(DirKit.STORE_LOGO);
					try {
						thumbFileName = uploadToFtp(dir,busthumb);
						deleteFromFtp(busthumbOld);
						bustFileName = uploadToFtp(dirr,perthumb);
						deleteFromFtp(perthumbOld);
					} catch (IOException e) {
						e.printStackTrace();
						renderResult(18);
						return;
					}
					seller.set("busthumb", dir.concat(thumbFileName));
					seller.set("perthumb", dirr.concat(bustFileName));
				}
				if(seller.update()){
					//renderSuccess(storeService.findById(store.getInt("id")));
					redirect("/dict/seller/index");
					return;
				}
			}
		renderFailed(); 
	  } 
	  
	  /**
	   * 查看驳回原因
	   */
	  public void toBoHui(){
			String sellerId=getPara(0);
	    	if(StrKit.notBlank(sellerId)){
	    		Seller seller=sellerService.findBySellerId(sellerId);
				String reason=seller.getStr("reason");
				setAttr("reason", reason);
				render("bohui.jsp");;
	    	}
	    	else{
	    		renderFailed("查看失败");
				return;
	        }
	    }
	  /**
	   * 编辑
	   */
	  public void edit(){
			String sellerId=getPara(0);
			sellerService.editBySellerId(sellerId);
			renderSuccess();
	  }
}
